package gdg.foursquare;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import commands.Command;
import commands.ui.CommandInUiThread;

import geo.GeoObj;
import gl.GL1Renderer;
import gl.GLCamera;
import gl.GLFactory;
import gui.v2.simpleUi.M_Caption;
import gui.v2.simpleUi.M_Container;
import gui.v2.simpleUi.M_InfoText;
import gui.v2.simpleUi.SimpleUI;
import system.ArActivity;
import system.DefaultARSetup;
import util.IO;
import util.Vec;
import worldData.World;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.graphics.Bitmap;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class GdgFoursquareActivity extends Activity {
	LinkedList<Venue> allVenues = new LinkedList<Venue>();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		Button b = (Button) findViewById(R.id.doh_button);

		b.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				ArActivity.startWithSetup(GdgFoursquareActivity.this,
						new DefaultARSetup() {

							@Override
							public void addObjectsTo(GL1Renderer renderer,
									final World world,
									final GLFactory objectFactory) {
								LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
								GeoObj o = new GeoObj();
								LocationListener ll = new LocationListener() {
									public void onStatusChanged(
											String provider, int status,
											Bundle extras) {
									}

									public void onProviderEnabled(
											String provider) {
									}

									public void onProviderDisabled(
											String provider) {
									}

									public void onLocationChanged(Location l) {
										generateExampleVenues(l);
										for (Venue venue : allVenues) {
											mLoadTask = new DownloadTask(world,
													objectFactory, camera, l, venue);
											mLoadTask.execute();
										}
										
									}
								};
								lm.requestLocationUpdates(
										LocationManager.NETWORK_PROVIDER, 0L,
										0f, ll);
								o.setComp(objectFactory.newArrow());
								// world.add(objectFactory.newSolarSystem(new
								// Vec(10,0,0)));

							}
						});
			}
		});

		Button b2 = (Button) findViewById(R.id.doh_button2);
		b2.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				showDialog(R.id.doh_button2);
			}
		});
	}

	private class DownloadTask extends AsyncTask<String, Integer, Void> {

		private Bitmap mImage;
		private World mWorld;
		private GLFactory mGl;
		private Location mLocation;
		private GLCamera mCamera;
		private String url_bild;
		private Venue venue;

		public DownloadTask(World w, GLFactory glFactory, GLCamera c,
				Location l, Venue venue) {
			mWorld = w;
			mGl = glFactory;
			mLocation = l;
			mCamera = c;
			this.venue = venue;
		}

		protected Void doInBackground(String... urls) {
			mImage = IO.loadBitmapFromURL(venue.url_bild);
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			Log.e("FFS", "funktioniert");
			Log.e("FFS", mLocation.toString());
			Location l = mLocation;
			GeoObj bla = mGl.newIconFacingToCamera(venue.lat,
					venue.lng, mImage, "fwejfi", 10.0f,
					mCamera);
			bla.setOnClickCommand(new CommandInUiThread() {
				@Override
				public void executeInUiThread() {
					Toast.makeText(GdgFoursquareActivity.this, "webTech",
							Toast.LENGTH_LONG).show();
					M_Container c = new M_Container();
					c.add(new M_Caption("Blabla"));
					c.add(new M_InfoText("abc"));
					SimpleUI.showInfoDialog(getApplicationContext(), "Ok", c);
				}
			});
			mWorld.add(bla);
		}
	}

	private DownloadTask mLoadTask;

	private void generateExampleVenues(Location l) {
		this.allVenues
				.add(new Venue(
						"http://polpix.sueddeutsche.com/polopoly_fs/1.589138.1273516148!/image/image.jpg_gen/derivatives/900x600/image.jpg",
						"www.bild.de", l.getLatitude(), l.getLongitude()));
		this.allVenues
				.add(new Venue(
						"http://images.tagseoblog.de/bilder/seo-sem-optimieren/seo-bilder.jpg",
						"www.google.de", l.getLatitude() + 0.00000001, l.getLongitude() + 0.00000001));
		this.allVenues
				.add(new Venue(
						"http://www.hd-gbpics.de/gbbilder/schoene-bilder/schoene-bilder3.jpg",
						"www.hd-gbpics.de", l.getLatitude() + 0.0001, l.getLongitude() + 0.0001));

	}
}